package novel.impl.chapter;

import novel.entity.Chapter;
import novel.entity.ChapterSynopsis;
import novel.enums.NovelSpiderEnum;
import novel.impl.AbstractCrawlSpider;
import novel.intrerfaces.IChapterSynopsisSpider;
import novel.intrerfaces.IchapterSpider;
import novel.util.NovelSpiderUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:xdw
 * @Date:Created in 上午 11:032019/3/7 0007
 * @described: 定义抽象类，实现获取章节列表接口
 * @Modified By:
 */
public abstract class AbstractChapterSpider extends AbstractCrawlSpider implements IchapterSpider ,IChapterSynopsisSpider {

    @Override
    public List<Chapter> getsChapter(String url) {
        try {
            String result=crawl(url);
            Document doc=Jsoup.parse(result);
            Elements els=doc.select(NovelSpiderUtil.getContext(NovelSpiderEnum.getEnumByUrl(url)).get("chapter-list-selector"));
            List<Chapter> chapterList=new ArrayList<>();
            for (Element e :els){
                Chapter chapter=new Chapter();
                chapter.setTitle(e.text());
                String href = StringUtil.resolve(url,e.attr("href"));
                chapter.setUrl(href);
                chapterList.add(chapter);
            }
            return chapterList;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public ChapterSynopsis getSynopsis(String url) {
        try {
            String result=crawl(url);
            //解析时将<br/>替换成${n}
            result = result.replace("&nbsp;", " ")
                    .replace("<br />", "${n}")
                    .replace("<br/>", "${n}");
            Document doc = Jsoup.parse(result);
            Map<String, String> context = NovelSpiderUtil.getContext(NovelSpiderEnum.getEnumByUrl(url));
            ChapterSynopsis chapterSynopsis=new ChapterSynopsis();
            //获取标题解析规则
            String[] titleSelector = context.get("title").split("\\,");
            titleSelector = parseSelect(titleSelector);
            chapterSynopsis.setTitle(doc.select(titleSelector[0])
                    .get(Integer.parseInt(titleSelector[1])).text().replace("${n}","\n"));
            //作者
            String[] authors=context.get("author").split("\\,");
            authors = parseSelect(authors);
            chapterSynopsis.setAuthor(doc.select(authors[0]).
                    get(Integer.parseInt(authors[1])).text().replace("${n}","\n"));

            //简介
            String intro=context.get("intro");
            chapterSynopsis.setSynopsis(doc.select(intro).text().replace("${n}","\n"));
            return chapterSynopsis;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //截取
    private String[] parseSelect(String[] spites) {
        String[] newSpites = new String[2];
        if (spites.length == 1) {
            newSpites[0] = spites[0];
            newSpites[1] = "0";
        }else {
            newSpites=spites;
        }
        return newSpites;
    }
}
